package br.usp.icmc.biomac.client.view.lixo;

import java.util.ArrayList;
import java.util.List;

import br.usp.icmc.biomac.shared.CSVData;
import br.usp.icmc.biomac.shared.CSVSerie;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;


public class CustomTreeModel implements TreeViewModel {

	private List<CSVData> cvsData;

	@SuppressWarnings("unused")
	private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();

	public CustomTreeModel() {
		// Create a database of information.
		cvsData = new ArrayList<CSVData>();

		List<HasCell<CSVData, ?>> hasCells = new ArrayList<HasCell<CSVData, ?>>();
		hasCells.add(new HasCell<CSVData, String>() {
			ClickableTextCell cell = new ClickableTextCell();

			@Override
			public Cell<String> getCell() {
				// TODO Auto-generated method stub
				return cell;
			}

			@Override
			public FieldUpdater<CSVData, String> getFieldUpdater() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getValue(CSVData object) {
				// TODO Auto-generated method stub
				return null;
			}
		});
	}

	public void setCvsData(List<CSVData> arrayList) {
		cvsData = arrayList;
	}

	/**
	 * Get the {@link NodeInfo} that provides the children of the specified
	 * value.
	 */
	@Override
	public <T> NodeInfo<?> getNodeInfo(T value) {
		if (value == null) {
			// LEVEL 0.
			// We passed null as the root value. Return the cvsData.

			// Create a data provider that contains the list of cvsData.
			ListDataProvider<CSVData> dataProvider = new ListDataProvider<CSVData>(
					cvsData);

			// Create a cell to display a composer.
			Cell<CSVData> cell = new AbstractCell<CSVData>() {
				@Override
				public void render(Context context, CSVData value,
						SafeHtmlBuilder sb) {
					if (value != null) {
						sb.appendHtmlConstant("<table class='tableTree'><tr>");
						sb.appendHtmlConstant("<td width=\"130\">");
						sb.appendEscaped(value.getStudyUID());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td width=\"100\">");
						sb.appendEscaped(value.getPatientName());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td width=\"20\">");
						sb.appendEscaped(value.getPatientID());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td width=\"25\">");
						sb.appendEscaped(value.getExamType());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td width=\"35\">");
						sb.appendEscaped(value.getDateAcquired().toString());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("</tr></table>");
					}
				}
			};

			// Return a node info that pairs the data provider and the cell.
			return new DefaultNodeInfo<CSVData>(dataProvider, cell);

		} else if (value instanceof CSVData) {
			// LEVEL 1.
			// We want the children of the composer. Return the playlists.
			ListDataProvider<CSVSerie> dataProvider = new ListDataProvider<CSVSerie>(
					((CSVData) value).getSeries());
			Cell<CSVSerie> cell = new AbstractCell<CSVSerie>() {
				@Override
				public void render(Context context, CSVSerie value,
						SafeHtmlBuilder sb) {
					if (value != null) {
						sb.appendHtmlConstant("<table class='tableTree'><tr>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getSeriesId());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getPatientId());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getPatientName());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getSeriesDate());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getExamType());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getSeriesDescription());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value
								.getNumberOfSeriesRelatedInstances());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("<td>");
						sb.appendEscaped(value.getImagesInSeries());
						sb.appendHtmlConstant("</td>");
						sb.appendHtmlConstant("</tr></table>");
					}
				}
			};

			return new DefaultNodeInfo<CSVSerie>(dataProvider, cell);

		}
		return null;
	}

	/**
	 * Check if the specified value represents a leaf node. Leaf nodes
	 * cannot be opened.
	 */
	@Override
	public boolean isLeaf(Object value) {
		// The leaf nodes are the songs, which are Strings.
		if (value instanceof CSVSerie) {
			return true;
		}
		return false;
	}

}
